package com.fofia.iotweb.web.domain.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.fofia.iotweb.web.domain.mappers.FcDataMapper;
import com.fofia.iotweb.web.domain.models.FcData;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

@Component
public class FcDataService {
    @Inject("fctag")
    private FcDataMapper _fcdataMapper;


    /**
     * 分页列表，包含总记录数total
     * @param tagId
     * @param page
     * @param psize
     * @param startTime
     * @param endTime
     * @return
     */
    public DataTableDto getPageList(String tagId, int page, int psize, String startTime, String endTime){
        String tableName= StrUtil.format("t_{}",tagId);
        DataTableDto  dto=new DataTableDto();
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.from(tableName);
        queryWrapper.where("upload_time > '"+startTime+"' and upload_time <'"+endTime+"'");
        queryWrapper.orderBy("upload_time desc");
        //String sql=queryWrapper.toSQL();
        //System.out.println(sql);
        Page<FcData> users=_fcdataMapper.paginate(page,psize,queryWrapper);
        var list=users.getRecords();
        for(FcData fcData : list) {
            fcData.setTagId(tagId);
        }
        dto.setRows(list);
        dto.setTotal(users.getTotalRow());
        dto.setPageNumber(page);
        dto.setPageSize(psize);
        //成功率
        DateTime date1 = DateUtil.parse(startTime);
        DateTime date2 = DateUtil.parse(endTime);
        long totalSeconds = DateUtil.between(date1, date2, DateUnit.SECOND);
        double totalInfo = totalSeconds * 1.5; //新标签大约1秒1.5包
        double total=dto.getTotal()*100.00/totalInfo;
        //double rate = Math.round(total * 1.00);
        String rate= String.format("%.2f", total);
        dto.setMessage(rate);
        return dto;
    }

    /**
     *历史数据，按时间升序
     * @param tagId 标签id
     * @param page  页码
     * @param psize 每页数量
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    public List<FcData> getHistoryList(String tagId,int page,int psize,String startTime,String endTime){
        String tableName= StrUtil.format("t_{}",tagId);
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.from(tableName);
        queryWrapper.where("upload_time > '"+startTime+"' and upload_time <'"+endTime+"'");
        queryWrapper.orderBy("upload_time");
        queryWrapper.limit((page-1)*psize,psize);
        //String sql=queryWrapper.toSQL();
        //System.out.println(sql);
        List<FcData> list=_fcdataMapper.selectListByQuery(queryWrapper);
        //Page<FcData> list=_fcdataMapper.paginate(page,psize,queryWrapper);

        return list;
    }
    /**
     *历史数据，按时间降序
     * @param tagId 标签id
     * @param page  页码
     * @param psize 每页数量
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    public List<FcData> getHistoryDescList(String tagId,int page,int psize,String startTime,String endTime){
        String tableName= StrUtil.format("t_{}",tagId);
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.from(tableName);
        queryWrapper.where("upload_time > '"+startTime+"' and upload_time <'"+endTime+"'");
        queryWrapper.orderBy("upload_time desc");
        queryWrapper.limit((page-1)*psize,psize);
        //String sql=queryWrapper.toSQL();
        //System.out.println(sql);
        List<FcData> list=_fcdataMapper.selectListByQuery(queryWrapper);
        //Page<FcData> list=_fcdataMapper.paginate(page,psize,queryWrapper);

        return list;
    }
}
